package com.produce.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.produce.common.util.ValidateUtils;
import com.produce.system.constans.SysMenuConstants;
import com.produce.system.entity.SysMenu;
import com.produce.system.service.SysMenuService;

@Controller
@RequestMapping(value = "sysmenu")
public class SysMenuController {
	private Logger logger = Logger.getLogger(SysMenuController.class);
	@Resource
	private SysMenuService sysMenuService;
	/**
	 * 初始化菜单的页面
	 */
	@RequestMapping(value = "init")
	public ModelAndView initUser(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		logger.info("初始化菜单页面");
		return new ModelAndView(SysMenuConstants.INITMENU,modelMap);
	}
	/**
	 * 菜单出列表
	 */
	@RequestMapping(value = "list")
	public ModelAndView listUser(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap){
		logger.info("出菜单列表");
		request.setAttribute("menuList", sysMenuService.findForTreeTable("000000"));
		return new ModelAndView(SysMenuConstants.MENULIST,modelMap);
	}
	/**
	 * 查询下级菜单
	 * @param parentId
	 * @return
	 */
	@RequestMapping("/treeTable")
	@ResponseBody
	public String treeTable(String parentid){
		JSONArray json = new JSONArray();
		try {
			logger.info("点击  + 展开菜单"+parentid);
			json.addAll(sysMenuService.findForTreeTable(parentid));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return json.toString();
	}
	/**
	 * 进新增和修改的页面
	 */
	@RequestMapping("/menu")
	public ModelAndView menu(String  umresourceid, HttpServletRequest request, ModelMap modeMap) {
		try {
			if (ValidateUtils.isNotEmpty(umresourceid)) {//进修改  带menu
				logger.info("带id进菜单修改页面 " + umresourceid);
				request.setAttribute("menu", sysMenuService.getById(umresourceid));
			}else{ // 进新增  带排序号
				SysMenu menu = new SysMenu();
				logger.info("菜单新增页面   查询最大排序号+10");
				menu.setSortnum(sysMenuService.findMaxSortNum());
				request.setAttribute("menu", menu);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return new ModelAndView(SysMenuConstants.MENU,modeMap);
	}
	/**
	 * 获取菜单树状结构   菜单新增   父级菜单
	 */
	@RequestMapping("/getMenuForTree")
	@ResponseBody
	public String getMenuForTree(String currentId){
		JSONArray json = new JSONArray();
		List<SysMenu> menuList = sysMenuService.find(new SysMenu());
		if(menuList != null && menuList.size() > 0){
			for(SysMenu menu : menuList){
				Map<String, Object> map = new HashMap<String, Object>();
				logger.info("菜单新增的 弹窗选父级节点树结构----"+menu.getUmresourcename());
				//Ztree 解析默认字段 id name pId   
				map.put("id", menu.getUmresourceid());
				map.put("name", menu.getUmresourcename());
				map.put("pId", menu.getUmresourceparentid());
				json.add(map);
			}
		}
		return json.toString();
	}
	/**
	 * 保存数据
	 * @param menu
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(SysMenu menu){
		JSONObject json = new JSONObject();
		try {
			if(ValidateUtils.isEmpty(menu.getUmresourceparentid())){
				menu.setUmresourceparentid("000000");
			}
			// 修改
			if(ValidateUtils.isNotEmpty(menu.getUmresourceid())){
				logger.info("菜单修改开始"+menu.getUmresourcename());
				sysMenuService.update(menu);
			}
			// 新增
			else{
				logger.info("菜单新增开始"+menu.getUmresourcename());
				sysMenuService.add(menu);
			}
			json.put("result", "save_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}
		return json.toString();
	}
	/**
	 * 删除数据 菜单
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(String umresourceid){
		JSONObject json = new JSONObject();
		try {
			logger.info("删除菜单操作开始  包含所有子菜单"+umresourceid);
			sysMenuService.delete(umresourceid);
			json.put("result", "delete_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}
		return json.toString();
	}
}
